//
//  ConversationAndShowBackViewController.swift
//  宜居Ipad
//
//  Created by URANUS on 15/10/22.
//  Copyright © 2015年 URANUS. All rights reserved.
//

import Foundation
import UIKit


public class ConversationAndShowBackViewController:UIViewController{
    private let OutSightPhoto=100
    private let HouseTypePhoto=101
    private let ObjectivePhoto=102
    private let TrafficPhoto=103
    private let ModelroomPhoto=104
    private let MaplocationPhoto=105
    private let ZBarPhoto = 106
    private let caculateTag = 107
    
    private var computerModelArray:[ShowComputerModels]!
    private var width:CGFloat!
    private var height:CGFloat!
    private var leftButtonGroupWidth:CGFloat!
    private var leftButtonGroupHeight:CGFloat!
    private var leftButtonGroupY:CGFloat!
    private var photoBrowser:ConversationAndShowPhotoBrowser!
    private var caculateView:HouseLoanCalculateViewController!
    
    private var computerButtons:[UIButton]!
    
    
    private var outSightPhotos:NSMutableArray!
    private var houseTypePhotos:NSMutableArray!
    private var objectivePhotos:NSMutableArray!
    private var trafficPhotos:NSMutableArray!
    private var modelroomPhotos:NSMutableArray!
    private var maplocationPhotos:NSMutableArray!
    private var zbarPhotos:NSMutableArray!
    
    private var leftButtonGroupTitle:UITextView = UITextView()
    private var leftButtonGroupTopImageView:UIImageView = UIImageView()
    
    public override func viewWillAppear(animated: Bool) {
        self.navigationController?.navigationBarHidden=true
    }
    
    public override func viewDidLoad() {
        super.viewDidLoad()
        width=self.view.frame.size.width-100
        height=self.view.frame.size.height
        self.view.backgroundColor=UIColor.whiteColor()
        self.automaticallyAdjustsScrollViewInsets = false;
        self.initContentView()
        let topView:UIView = UIView()
        topView.frame = CGRectMake(0, 0, self.view.frame.size.width, 20)
        topView.backgroundColor = UIColor.init(red: 41/255.0, green: 53/255.0, blue: 65/255.0, alpha: 1.0)
        self.view.addSubview(topView)
    }
    
    func initContentView(){
        
        self.initLeftButtonGroup()
        
        self.initPhotoBrowser()
    }
    
    
    func initTitleScrollView(){
        let titleScroll:UIScrollView = UIScrollView()
        titleScroll.frame=CGRectMake(0, 64,width,height*0.15)
        titleScroll.alwaysBounceHorizontal=true
        titleScroll.showsHorizontalScrollIndicator=true
        titleScroll.userInteractionEnabled = true;
        computerModelArray=[ShowComputerModels]()
        computerButtons=[UIButton]()
        
//        let dirPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomainMask.UserDomainMask, true)
//        let filePath = dirPath[0] + "/PosCollectionViewInfo.plist"
//        let fileManager = NSFileManager .defaultManager()
//        
//        if fileManager.fileExistsAtPath(filePath) {
//            
//            var myDict: NSDictionary?
//            myDict = NSDictionary(contentsOfFile: filePath)
//            if let dict = myDict {
//                let computerModels:[Dictionary<String,String>]=dict["computerSetUp"] as! [Dictionary<String,String>]
//                titleScroll.contentSize=CGSizeMake(width*(CGFloat)(computerModels.count/6+1), height*0.15)
//                for index in 0..<computerModels.count{
//                    print("\(index)")
//                    let computerModelDic:Dictionary<String,String> = computerModels[index]
//                    let showComputerModels:ShowComputerModels=ShowComputerModels()
//                    showComputerModels.setValuesForKeysWithDictionary(computerModelDic)
//                    computerModelArray.append(showComputerModels)
//                    let computerButton:UIButton=self.addScrollButton(index, btnCount: computerModels.count)
//                    let computerName:UILabel = UILabel.init(frame: CGRectMake(computerButton.frame.origin.x, computerButton.frame.origin.y+computerButton.frame.size.height+5, computerButton.frame.size.width, 15))
//                    computerName.text=showComputerModels.computerName
//                    computerName.textAlignment=NSTextAlignment.Center
//                    computerName.font=UIFont.systemFontOfSize(12)
//                    titleScroll.addSubview(computerName)
//                    titleScroll.addSubview(computerButton)
//                    computerButtons!.append(computerButton)
//                }
//            }
//        }
        
//        self.view.addSubview(titleScroll)
    }
    
//    func addScrollButton(tag:Int,btnCount:Int)->UIButton{
//        
//        let button:UIButton = UIButton.init(type: UIButtonType.Custom)
//        button.setBackgroundImage(UIImage.init(named:"ComputerNot.png"), forState: UIControlState.Normal)
//        button.setBackgroundImage(UIImage.init(named: "ComputerYes.png"), forState: UIControlState.Selected)
//        button.layer.masksToBounds=true
//        button.layer.cornerRadius=3
//        button.tag=tag
//        
//        let inset:CGFloat
//        if(btnCount<5){
//            inset=(width-(CGFloat)(btnCount*100))/(CGFloat)(btnCount+1)
//        }else{
//            inset=(width-(CGFloat)(4*100))/(CGFloat)(5)
//        }
//        let btnX:CGFloat=inset*(CGFloat)(tag+1)+(CGFloat)(tag*100)
//        print("测试数量\(width),\(btnCount),\(inset),\(btnX)")
//        button.frame=CGRectMake(btnX ,height*0.02, 100, height*0.1)
//        button.addTarget(self, action: #selector(ConversationAndShowBackViewController.selectComputerButtonAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)
//        return button
//    }
    
//    func selectComputerButtonAction(btnComputer:UIButton){
//        
//        for index in 0..<computerButtons.count {
//            
//            let btn:UIButton=computerButtons[index]
//            if btn != btnComputer {
//                
//                btn.selected=false
//                
//            }
//        }
//        
//        if btnComputer.selected == true {
//            
//            btnComputer.selected=false
//            
//        }else{
//            
//            btnComputer.selected=true
//        }
//        
//    }
    func initLeftButtonGroup(){
        leftButtonGroupWidth=width*0.16
        leftButtonGroupHeight=height
        leftButtonGroupY=0
        let leftButtonGroupBackView:UIScrollView = UIScrollView(frame: CGRectMake(0, leftButtonGroupY,leftButtonGroupWidth, leftButtonGroupHeight))
        leftButtonGroupBackView.backgroundColor=UIColor.init(red: 218/255.0, green: 221/255.0, blue: 234/255.0, alpha: 1.0)
        self.view.addSubview(leftButtonGroupBackView)
        
        let leftButtonWidth:CGFloat=0.8*leftButtonGroupWidth
        let leftButtonX:CGFloat=(leftButtonGroupWidth-leftButtonWidth)/2
        let leftButtonHeight:CGFloat=leftButtonWidth/3.3
        let leftButtonGroupInset:CGFloat=leftButtonGroupHeight/30
       
        
        leftButtonGroupTopImageView = UIImageView.init(frame: CGRectMake(leftButtonX, leftButtonGroupInset+20, leftButtonWidth*0.8, leftButtonWidth*0.8))
        leftButtonGroupTopImageView.center = CGPointMake(leftButtonGroupWidth/2, leftButtonGroupInset+20+leftButtonWidth*0.4)
        leftButtonGroupTopImageView.backgroundColor = UIColor.redColor()
        leftButtonGroupTopImageView.contentMode = UIViewContentMode.ScaleAspectFill
        leftButtonGroupBackView.addSubview(leftButtonGroupTopImageView)
        
        leftButtonGroupTitle = UITextView(frame: CGRectMake(leftButtonX,leftButtonGroupInset+leftButtonWidth*0.8+20, leftButtonWidth, leftButtonHeight*2))
        leftButtonGroupTitle.font = UIFont.systemFontOfSize(16)
        leftButtonGroupTitle.textAlignment=NSTextAlignment.Center
        leftButtonGroupTitle.backgroundColor=UIColor.clearColor()
        leftButtonGroupTitle.textColor = UIColor.init(red: 60/255.0, green: 60/255.0, blue: 60/255.0, alpha: 1.0)
        leftButtonGroupTitle.editable = false;
        leftButtonGroupBackView.addSubview(leftButtonGroupTitle)
        
        let titleArray:[String] = ["外景图","户型图","实景图","交通图","样板间","地图位置","显示二维码"]
        
        let contentHeight:CGFloat = leftButtonHeight * (CGFloat)(titleArray.count) + leftButtonGroupInset * (CGFloat)(titleArray.count) + (CGFloat)(leftButtonGroupTitle.frame.origin.y) + (CGFloat)(leftButtonGroupTitle.frame.size.height) + 50
        
        if contentHeight > leftButtonGroupHeight{
            leftButtonGroupBackView.contentSize=CGSizeMake(leftButtonGroupWidth, contentHeight)
        }else{
            
            leftButtonGroupBackView.contentSize=CGSizeMake(leftButtonGroupWidth, leftButtonGroupHeight)
        }
        
        for index in 0..<titleArray.count {
            
            let leftButtonTag:Int=index+100
            let leftButton=self.addLeftButtonGroupButton(leftButtonTag, title:titleArray[index], inset: leftButtonGroupInset, btnX: leftButtonX, width: leftButtonWidth, height: leftButtonHeight)
            leftButtonGroupBackView.addSubview(leftButton)
        }
        
        let caculateBtn:UIButton = UIButton.init(type: UIButtonType.Custom)
        caculateBtn.backgroundColor = UIColor.redColor();
        caculateBtn.setImage(UIImage(named: "Profile"), forState: UIControlState.Normal)
        caculateBtn.setTitle("计算器", forState: UIControlState.Normal)
        caculateBtn.titleLabel?.font = UIFont.systemFontOfSize(15)
        caculateBtn.tag = 107
        caculateBtn.addTarget(self, action: #selector(updatePhotoBrowser(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        caculateBtn.frame = CGRectMake(leftButtonX, leftButtonGroupHeight - leftButtonHeight - 20, leftButtonWidth, leftButtonHeight)
        leftButtonGroupBackView.addSubview(caculateBtn)
    }
    
    func addLeftButtonGroupButton(tag:Int,title:String,inset:CGFloat,btnX:CGFloat,width:CGFloat,height:CGFloat)->UIButton{
        
        let button:UIButton = UIButton.init(type: UIButtonType.Custom)
        button.layer.masksToBounds=true
        button.layer.cornerRadius=3
        button.tag=tag
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        let btnY:CGFloat=inset*(CGFloat)(tag-98)+(CGFloat)(tag-99)*height+height*3
        button.frame=CGRectMake(btnX, btnY, width, height)
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.init(red: 80/255.0, green: 80/255.0, blue: 80/255.0, alpha: 1.0), forState: UIControlState.Normal)
        button.addTarget(self, action: #selector(updatePhotoBrowser(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }
    
    func initPhotoBrowser(){
        
        photoBrowser = ConversationAndShowPhotoBrowser()
        photoBrowser.browserRect=CGRectMake(leftButtonGroupWidth+20,leftButtonGroupY+10 , width-leftButtonGroupWidth-20, leftButtonGroupHeight)
        self.view.addSubview(photoBrowser.view)
        self.addChildViewController(photoBrowser)
        
        getAllPhotos()
        
    }
    
    func getAllPhotos(){

        let postDic:Dictionary<String,AnyObject> = Dictionary()
//        postDic["type"] = "50078"
//        postDic["proid"] = UserDefaultsData.getProid()
//        postDic["salerid"] = UserDefaultsData.getSalerId()
        let postString:String = CommFunc.dictionaryToJson(postDic)
        print("洽谈演示图片浏览器发送数据：\(postString)")
//        let serverHandle:ServerHandle = ServerHandle()
//        serverHandle.requestHandler(postString) { (response) -> Void in
//            print("洽谈演示图片浏览器收到数据：\(response)")
//            let responseDic:Dictionary<NSObject,AnyObject> = CommFunc.dictionaryWithJsonString(response)
//            let status:String=responseDic["status"] as! String
//            if status == "true" {
//                
//                let projectLogo:NSMutableArray = responseDic["projectLogo"] as! NSMutableArray
//                self.leftButtonGroupTopImageView.sd_setImageWithURL(NSURL.init(string:  CommFunc.getImageURL(projectLogo[0] as! String) ))
//                let projectName = responseDic["projectName"] as! String
//                self.leftButtonGroupTitle.text = projectName
        
                self.outSightPhotos=["1","2","3","4"] 
                self.houseTypePhotos=["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"]
                self.objectivePhotos=["1","2"]
                self.trafficPhotos=["1","2","3"]
                self.modelroomPhotos=["1","2"]
                self.maplocationPhotos=["1","2","2","3","4"]
//                self.zbarPhotos = responseDic["QRCode"] as? NSMutableArray
        
                let button:UIButton = self.view.viewWithTag(100) as! UIButton
                self.updatePhotoBrowser(button)
//
//            }
//        }
    }
    
    func initCaculateView() {
        caculateView = HouseLoanCalculateViewController()
        caculateView.caculateRect=CGRectMake(leftButtonGroupWidth,leftButtonGroupY+20 , width-leftButtonGroupWidth+10, leftButtonGroupHeight)
//        let nav = UINavigationController(rootViewController: caculateView);
//        self.view.addSubview(nav.view)
//        self.addChildViewController(nav)
        self.view.addSubview(caculateView.view)
        self.addChildViewController(caculateView)
    }
    
   func updatePhotoBrowser(photoButton:UIButton){
        photoBrowser.view.hidden = false
        if caculateView != nil {
            caculateView.view.removeFromSuperview()
            caculateView.removeFromParentViewController()
        }
        for i in 100 ..< 108 {
          
            let button:UIButton = self.view.viewWithTag(i) as! UIButton
            button.setTitleColor(UIColor.init(red: 80/255.0, green: 80/255.0, blue: 80/255.0, alpha: 1.0), forState: UIControlState.Normal)
            
        }
        photoButton.setTitleColor(UIColor.init(red: 255/255.0, green: 108/255.0, blue: 96/255.0, alpha: 1.0), forState: UIControlState.Normal)
        switch photoButton.tag {
            
        case OutSightPhoto :
            photoBrowser.updatePhotos(self.outSightPhotos)
        case HouseTypePhoto :
            photoBrowser.updatePhotos(self.houseTypePhotos)
        case ObjectivePhoto :
            photoBrowser.updatePhotos(self.objectivePhotos)
        case TrafficPhoto :
            photoBrowser.updatePhotos(self.trafficPhotos)
        case ModelroomPhoto :
            photoBrowser.updatePhotos(self.modelroomPhotos)
        case MaplocationPhoto :
            photoBrowser.updatePhotos(self.maplocationPhotos)
        case ZBarPhoto :
            photoBrowser.updatePhotos(self.zbarPhotos)
        case caculateTag :
            photoBrowser.view.hidden = true;
            self.initCaculateView()
        default :
            print("点击异常")
        }
        
    }
    
}